<?php

class MapModel extends Model {

	function MapModel()
	{
			// Call the Model constructor
			parent::Model();
	}
		
		
	/**
	 * load Geo Markers from database
	 *
	 * @access	public
	 * @return	array
	 */	
	function loadGeoMarkers()
	{		
		// get the positions and the tags associated
		$query = $this->db->query(
			"SELECT DISTINCT g.x, g.y, g.id_note, tn.name, tn.filename
			 FROM bnm_geo g, bnm_notes n, bnm_tags t, bnm_tagnames tn
			 WHERE g.id = n.id_geo 
			 AND n.id = t.id_note 
			 AND t.id_tag = tn.id 
			 AND g.id_note IS NOT NULL
			");
		return $query->result();
	}	
		
	/**
	 * Load Photo Bar
	 *
	 * Load 18 photos from database. => bar of 900px width
	 *
	 * @access	public
	 * @return	array
	 */	
	function loadPhotoBar()
	{
		// @todo (Leopold) : change order by id by another thing, such as 
		// Put in the admin panel a way to chose the photos
		// in the bar
		
		//$this->db->select('photos.id,photos.id_note,photos.filename,geo.x,geo.y');
		$this->db->select('photos.id,photos.id_note,photos.filename');
		$this->db->from('photos');
		//$this->db->join('geo','photos.id_geo = geo.id');
		$this->db->order_by('photos.id','DESC');
		$this->db->limit('16');
		
		$query = $this->db->get();
		$dbPhotos = $query->result();
		$photos = array();
		foreach($dbPhotos as $photo)
		{
			$photos[] = array('id'=>$photo->id,
												'src'=>$this->config->item('dir_photos_min').$photo->filename,
											  'id_note'=>$photo->id_note) ;
		}
		
		return $photos;
	}
	
	function loadPhotosMarkers()
	{
		$this->db->select('photos.id,photos.id_note,photos.filename,geo.x,geo.y');
		$this->db->from('photos');
		$this->db->join('geo','photos.id_geo = geo.id');
		
		$query = $this->db->get();
		$dbPhotos = $query->result();
		$photos = array();
		foreach($dbPhotos as $photo)
		{
			$photos[] = array('id'=>$photo->id,
												'src'=>$this->config->item('dir_photos_min').$photo->filename,
											  'id_note'=>$photo->id_note,
												'x'=>$photo->x,
												'y'=>$photo->y);
		}
		return $photos;
	}
	
	function loadTagBar()
	{
		$this->db->select('tagnames.id,tagnames.name,tagnames.filename');
		$this->db->from('tagnames');
		
		$query = $this->db->get();
		$dbTag = $query->result();
		
		foreach($dbTag as $lineTag)
		{
			$tag[] = array(
				'id'=>$lineTag->id,
				'name'=>$lineTag->name,
				'src'=>$this->config->item('dir_tag').$lineTag->filename
				);
		}
		return $tag;
	}

}
?>